package hrms.controller;

import hrms.entity.Article;
import hrms.entity.Result;
import hrms.entity.Sort;
import hrms.entity.SortUtil;
import hrms.service.ArticleService;
import hrms.service.SortService;
import hrms.util.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sort")
public class SortController {
    @Autowired
    SortService sortService;

    @Autowired
    ArticleService articleService;

    @GetMapping("/list")
    public Result getSorts(){
        List<Sort> sortList = sortService.findAllSorts();
        if(sortList == null){
            return Result.fail().error("暂无分类数据");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("sortList", sortList);
        return Result.success().addMap(map);
    }

    @PostMapping("/add")
    public Result addSort(String name){
        Sort sort = sortService.findSortByName(name);
        if(sort != null){
            return Result.fail().error("已存在该分类，创建失败");
        }
        sort = new Sort();
        sort.setName(name);
        sort.setDescription("");
        sortService.addSort(sort);
        return Result.success();
    }

    @PostMapping("/articleList")
    public Object findPage(@RequestBody SortUtil sortUtil) {
        System.out.println(sortUtil.toString());
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(sortUtil.getPageNum());
        pageRequest.setPageSize(5);
//        Sort sort = sortService.findSortByName(sortUtil.getName());
        return articleService.findPage(pageRequest, sortUtil.getId());
    }
}
